<?php
 if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Mclassify extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    
    public function getListClassify($number,$offset)
    {
        $sql = "SELECT * FROM `classify_device`";
        //$data = $this->db->query($sql)->result_object();
        //return $data;
        
        $limit = $offset ? intval($offset) : '0';
        $sql .= " ORDER BY id DESC";
        $xdata = " LIMIT ".$limit.",".$number." ";
        
        $xs = $this->db->query($sql.$xdata)->result_object();
        $count = $this->db->query($sql)->num_rows();
        return array('data' => $xs, 'count' => $count);
    }
    
    public function getSearchContent($number,$offset,$keyword)
    {
        $sql = "SELECT * FROM `classify_device` WHERE os_name like '%".$keyword."%' OR device_name like '%".$keyword."%' OR note like '%".$keyword."%'";
        //$data = $this->db->query($sql)->result_object();
        //return $data;
        
        $limit = $offset ? intval($offset) : '0';
        $sql .= " ORDER BY id DESC";
        $xdata = " LIMIT ".$limit.",".$number." ";
        
        $xs = $this->db->query($sql.$xdata)->result_object();
        $count = $this->db->query($sql)->num_rows();
        return array('data' => $xs, 'count' => $count);
    }
    
    public function addClassifyDb($os_name,$device_name,$note,$classify,$type)
    {
        $data = array(
            'group' => $classify,
            'os_name' => $os_name,
            'device_name' => $device_name,
            'note' => $note,
            'type' => $type
        );
        
        $count = $this->db->query("SELECT id FROM `classify_device` WHERE LOWER(os_name) = '".$os_name."' AND LOWER(device_name) = '".$device_name."'")->num_rows();
        if($count === 0){
            $this->db->insert('classify_device', $data);
            return 200;   
        }
        return 201;
    }
    
    public function getInfo($id)
    {
        $data = $this->db->query("SELECT * FROM `classify_device` WHERE id = ".$id)->row_object();
        return $data;
    }
    public function editClassifyDb($os_name,$device_name,$note,$classify,$type,$id)
    {
        $data = array(
            'group' => $classify,
            'os_name' => $os_name,
            'device_name' => $device_name,
            'note' => $note,
            'type' => $type
        );
        
        $count = $this->db->query("SELECT id FROM `classify_device` WHERE LOWER(os_name) = '".$os_name."' AND LOWER(device_name) = '".$device_name."' AND id != ".$id)->num_rows();
        if($count === 0){
            $this->db->update('classify_device', $data, "id =".$id);
            return 200;   
        }
        return 201;
    }
    
    public function deleteClassifyDb($id)
    {
        $this->db->delete('classify_device', array('id' => $id));
        return 200;
    }
}